package com.ruoyi.web.controller.net;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.net.domain.DeviceStatusAprNeighb;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.domain.PerformanceSwitchListVo;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.PerformanceSwitch;
import com.ruoyi.net.service.IPerformanceSwitchService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 性能管理-性能开关Controller
 * 
 * @author tony
 * @date 2024-12-19
 */
@Api(tags = "性能管理-性能开关")
@RestController
@RequestMapping("/performance/switch")
public class PerformanceSwitchController extends BaseController
{
    @Autowired
    private IPerformanceSwitchService performanceSwitchService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 读取性能管理-性能开关列表
     */
    @ApiOperation(value = "读取性能管理-性能开关列表", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:switch:list')")
    @GetMapping("/read")
    public TableDataInfo read(PerformanceSwitch performanceSwitch)
    {
        List<PerformanceSwitch> performanceSwitches = new ArrayList<>();
        StringBuilder xml = new StringBuilder();
        xml.append("<filter type=\"subtree\">" +
                "<pm-oper xmlns=\"http://fiberhome.com/fhnw/yang/systemmanage/fos-pm-oper\">" +
                "            <real-pm-states>" +
                "                <real-pm-state>" +
//                "                    <resource></resource>" +
//                "                    <enable></enable>" +
                "                </real-pm-state>" +
                "            </real-pm-states>" +
                "        </pm-oper>" +
                "</filter>");
        NetElement netElement = netElementService.getCacheObject(performanceSwitch.getElementId());
        if (sysConfigService.selectDebugEnabled()) {
            deviceManager.getDevice(netElement.getIp());
            String rpcReply = deviceManager.getStatus(xml.toString());
            // 解析xml
            NodeList list = findNetConfXmlNodes(rpcReply, "//<real-pm-states/<real-pm-state");
            if (list != null && list.getLength() > 0) {
                for (int i = 0; i < list.getLength(); i++) {
                    PerformanceSwitch entity = new PerformanceSwitch();
                    Node node = list.item(i);
                    // 通过标签名获取节点的值
                    entity.setLocation(getXmlLabelValue(node, "resource"));
                    entity.setIsEnable(getXmlLabelValue(node, "enable"));

                    performanceSwitches.add(entity);
                }
            }
        }
        startPage();
        return getDIYPageTable(performanceSwitches);
    }

    /**
     * 查询性能管理-性能开关列表
     */
    @ApiOperation(value = "查询性能管理-性能开关列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:switch:list')")
    @GetMapping("/list")
    public TableDataInfo list(PerformanceSwitch performanceSwitch)
    {
        startPage();
        List<PerformanceSwitchListVo> list = performanceSwitchService.selectPerformanceSwitchList(performanceSwitch);
        return getDataTable(list);
    }

    /**
     * 导出性能管理-性能开关列表
     */
    @ApiOperation(value = "导出性能管理-性能开关列表", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:switch:export')")
    @Log(title = "性能管理-性能开关", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PerformanceSwitch performanceSwitch)
    {
        List<PerformanceSwitchListVo> list = performanceSwitchService.selectPerformanceSwitchList(performanceSwitch);
        ExcelUtil<PerformanceSwitchListVo> util = new ExcelUtil<PerformanceSwitchListVo>(PerformanceSwitchListVo.class);
        util.exportExcel(response, list, "性能管理-性能开关数据");
    }

    /**
     * 获取性能管理-性能开关详细信息
     */
    @ApiOperation(value = "获取性能管理-性能开关详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:switch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(performanceSwitchService.selectPerformanceSwitchById(id));
    }

    /**
     * 新增性能管理-性能开关
     */
    @ApiOperation(value = "新增性能管理-性能开关", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:switch:add')")
    @Log(title = "性能管理-性能开关", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PerformanceSwitch performanceSwitch)
    {
        return toAjax(performanceSwitchService.insertPerformanceSwitch(performanceSwitch));
    }

    /**
     * 修改性能管理-性能开关
     */
    @ApiOperation(value = "修改性能管理-性能开关", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('net:switch:edit')")
    @Log(title = "性能管理-性能开关", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PerformanceSwitch performanceSwitch)
    {
        return toAjax(performanceSwitchService.updatePerformanceSwitch(performanceSwitch));
    }

    /**
     * 删除性能管理-性能开关
     */
    @ApiOperation(value = "删除性能管理-性能开关", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('net:switch:remove')")
    @Log(title = "性能管理-性能开关", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(performanceSwitchService.deletePerformanceSwitchByIds(ids));
    }
}
